//
//  WithdrawViewController.m
//  AppleMarket
//
//  Created by yangy on 06/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "WithdrawViewController.h"
// view
#import "WithdrawView.h"
// viewModel
#import "WithdrawViewModel.h"
// controller
#import "WithdrawAddressListViewController.h"
#import "InputCodeViewController.h"
#import "SafePwdAlterViewController.h"
#import "PullRecordViewController.h"
// model
#import "AddressModel.h"
#import "ImageCodeMoel.h"

@interface WithdrawViewController ()
@property (nonatomic, strong) WithdrawView *withdrawView;
@property (nonatomic, strong) WithdrawViewModel *viewModel;
@end

@implementation WithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    self.view.backgroundColor = [UIColor whiteColor];
    self.viewModel.coinModel = self.coinModel;
    [self.view addSubview:self.withdrawView];
    [self bindViewModel];
    [self setNagivationBar];
}

- (void) setNagivationBar
{
    UIButton *rightButton = [UIButton cm_ButtonWithTitle:@"提现记录" font:kFont(13) titleColor:kColorWithHex(0x4A647A)];
    rightButton.titleLabel.textAlignment = NSTextAlignmentRight;
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
       // 提现记录
        PullRecordViewController *recordVC = [[PullRecordViewController alloc] init];
        recordVC.coinModel = self.coinModel;
        [self.navigationController pushViewController:recordVC animated:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void) bindViewModel
{
    @weakify(self);
    [self.viewModel.addressClickSubject subscribeNext:^(id  _Nullable x) {
        // 选择提币地址
        @strongify(self);
        WithdrawAddressListViewController *addressVC = [[WithdrawAddressListViewController alloc] init];
        addressVC.assetModel = self.coinModel;
        addressVC.selectHandler = ^(AddressModel * _Nonnull addressModel) {
            [self.viewModel.selectAddressSubject sendNext:addressModel];
        };
        [self bh_PushViewController:addressVC hiddenTabbar:YES];
    }];
    
    [self.viewModel.sendCodeClickSubject subscribeNext:^(id  _Nullable x) {
        // 获取发送提现短信、邮箱图形验证码
        InputCodeViewController *codeVC = [InputCodeViewController inputCodeViewControllerWithCodeType:CodeTypeDoWithdraw];
        codeVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        codeVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        codeVC.completeHandler = ^(ImageCodeMoel * _Nonnull codeModel,NSString *inputCodeStr) {
            [self.viewModel.sendCodeCommand execute:RACTuplePack(codeModel.ImgKey,inputCodeStr)];
        };
        [self presentViewController:codeVC animated:YES completion:nil];
    }];
    
    [self.viewModel.commitClickSubject subscribeNext:^(id  _Nullable x) {
        SafePwdAlterViewController *pwdVC = [[SafePwdAlterViewController alloc] init];
        pwdVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
        pwdVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        pwdVC.completeHandler = ^(NSString * _Nonnull pwdStr_) {
            // 请求提现接口
            [self.viewModel.withdrawCommand execute:pwdStr_];
        };
        [self presentViewController:pwdVC animated:YES completion:nil];
    }];
    
    [self.viewModel.withdrawSuccessSubject subscribeNext:^(id  _Nullable x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (WithdrawView *)withdrawView
{
    if(!_withdrawView){
        _withdrawView = [[WithdrawView alloc] initWithFrame:self.view.bounds viewMode:self.viewModel];
    }
    return _withdrawView;
}

- (WithdrawViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[WithdrawViewModel alloc] init];
    }
    return _viewModel;
}


@end
